package com.ponhu.ea2.stockcenter.provider.service.biz.stockinit.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ponhu.ea2.common.basic.model.ResponseBean;
import com.ponhu.ea2.stockcenter.enums.ResponseCodeEnum;
import com.ponhu.ea2.stockcenter.provider.service.biz.stockinit.GWallStockDataService;
import com.ponhu.ea2.stockcenter.provider.support.exception.StockRuntimeException;
import com.ponhu.ea2.stockcenter.provider.support.vo.GWallStockDataVO;
import com.ponhu.ea2.stockcenter.provider.utils.HttpClientResult;
import com.ponhu.ea2.stockcenter.provider.utils.HttpClientUtils;
import com.ponhu.ea2.wmscenter.facade.bms.PhWarehouseFacade;
import com.ponhu.ea2.wmscenter.facade.bms.PlatformFacade;
import com.ponhu.ea2.wmscenter.model.bms.dto.GoodsStockInfoListQueryDto;
import com.ponhu.ea2.wmscenter.model.bms.dto.PhWarehouseDto;
import com.ponhu.ea2.wmscenter.model.bms.vo.GoodsStockInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.ponhu.ea2.stockcenter.enums.ResponseCodeEnum.JOIN_WMS_INIT_STOCK_DATA_EXCEPTION;

/**
 * 巨沃库存数据服务
 */
@Slf4j
@Service
public class GWallStockDataServiceImpl implements GWallStockDataService {

    @DubboReference(version = "1.0.0", check = false, timeout = 1800000)
    private PlatformFacade platformFacade;

    @DubboReference(version = "1.0.0", check = false, timeout = 60000)
    private PhWarehouseFacade phWarehouseFacade;

    @Override
    public List<GoodsStockInfoVo> getGWallStockDataNew(String warehouseCode, List<String> skuList) {
        GoodsStockInfoListQueryDto queryDto = new GoodsStockInfoListQueryDto();
        queryDto.setWarehouseCode(warehouseCode);
//        queryDto.setType("COMBINED_NO");
//        queryDto.setType("COMBINED_OPEN");
        queryDto.setType(StringUtils.isEmpty(warehouseCode) ? "COMBINED_ALL" : "COMBINED_NO");
        queryDto.setSkuList(skuList);

        ResponseBean<List<GoodsStockInfoVo>> listResponseBean = platformFacade.listGoodsStockInfo(queryDto);
        if (listResponseBean.getCode() == ResponseCodeEnum.SUCCESS.getCode()) {
            return listResponseBean.getData();
        } else {
            log.error("仓库:{} ====>> 货品编码: {} ====>> 调用WMS系统获取仓库库存数据失败：{}",
                    warehouseCode, JSONObject.toJSONString(skuList), listResponseBean.getMsg());
            //异常
            throw new StockRuntimeException(JOIN_WMS_INIT_STOCK_DATA_EXCEPTION);
        }
    }

    @Override
    public List<PhWarehouseDto> getAllWarehouseList() {
        try {
            ResponseBean<List<PhWarehouseDto>> listResponseBean = phWarehouseFacade.listWarehouse(null);
            if (ResponseCodeEnum.SUCCESS.getCode().equals(listResponseBean.getCode())) {
                log.info("调用WMS系统获取仓库列表成功");
                return listResponseBean.getData();
            } else {
                log.error("调用WMS系统获取仓库列表失败：{}", JSONObject.toJSONString(listResponseBean));
                //异常
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("调用WMS系统获取仓库列表异常 === error", e);
            return null;
        }
    }

}
